package summer.hot100.v1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution46 {
    List<List<Integer>> res = new ArrayList<>();

    List<Integer> tmp = new LinkedList<>();

    public List<List<Integer>> permute(int[] nums) {
        boolean[] used = new boolean[nums.length];
        backtrack(used, nums);
        return res;
    }

    public void backtrack(boolean[] used, int[] nums) {
        if (tmp.size() == nums.length) {
            res.add(new ArrayList<>(tmp));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!used[i]) {
                used[i] = true;
                tmp.add(nums[i]);
                backtrack(used, nums);
                tmp.remove(tmp.size() - 1);
                used[i] = false;
            }
        }
    }
}
